package com.shg.service.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Service;

import com.shg.bean.Shop;
import com.shg.bean.Users;
import com.shg.service.UsersService;
import com.shg.web.tools.MD5;

@Service("usersServiceBean")
public class UsersServiceBean extends BasiceDaoImpl<Users>  implements UsersService {

	public boolean getCheck(String emain) {
		String sql="from Users where upper(userEmail)=upper('"+emain+"')";
		Query query = sessionFactory.getCurrentSession().createQuery(sql);
		return query.list().isEmpty();
	}
	/**
	 * 注意这里的用户名是区分大小写的，密码区分大小写
	 */
	@SuppressWarnings("unchecked")
	public Users getLogin(String userEmail, String passWord) {
		String sql="from Users where upper(userEmail)=upper('"+userEmail+"')  and passWord='"+MD5.MD5Encode(passWord)+"'";
		List<Users> users= sessionFactory.getCurrentSession().createQuery(sql).list();
		//如果查询结果大于零就将查询结果的第一条返回（结果最多只可能只有一条）
		if(users.size()>0){
			Users user=users.get(0);
			Shop shop= (Shop) sessionFactory.getCurrentSession().get(Shop.class, user.getId());
			user.setShop(shop);
			return user ;
		}
		return null;
	}
	/**
	 * 修改密码
	 */
	public void updatePassWord(Users user) {
		String sql="update Users  set passWord='"+user.getPassWord()+"' where upper(userEmail)=upper('"+user.getUserEmail()+"')";
		sessionFactory.getCurrentSession().createSQLQuery(sql).executeUpdate();
	}
}
